﻿using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Threading;
using Request;

namespace Requestor.TestApp
{
    public class MainViewModel : ViewModelBase
    {
        public static MainViewModel This { get; private set; }

        public string Url
        {
            get { return _url; }
            set
            {
                _url = value;
                OnPropertyChanged("Url");
                SendCommand.RaiseCanExecuteChanged();
            }
        }
        private string _url;

        public HttpMethod RequestType
        {
            get { return _requestType; }
            set
            {
                _requestType = value;
                OnPropertyChanged("RequestType");
            }
        }
        private HttpMethod _requestType;

        public HttpMethod CurrentRequestType
        {
            get { return _currentRequestType; }
            set
            {
                _currentRequestType = value;
                OnPropertyChanged("CurrentRequestType");
            }
        }
        private HttpMethod _currentRequestType;

        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                OnPropertyChanged("IsChecked");
            }
        }
        private bool _isChecked;

        public ObservableCollection<string> Data
        {
            get { return _data; }
            set
            {
                _data = value;
                OnPropertyChanged("Data");
            }
        }
        private ObservableCollection<string> _data;

        public string Result { get; private set; }

        public MainViewModel(Dispatcher dispatcher) 
            : base(dispatcher)
        {
            This = this;
            SendCommand = new DelegateCommand(Send, CanSend);
            AddParameterCommand = new DelegateCommand(AddParameter, CanAddParameter);
            IsChecked = true;
            Data = new ObservableCollection<string>();
        }

        #region SendCommand

        public DelegateCommand SendCommand { get; private set; }

        private void Send(object sender)
        {
            Result = null;
            var data = new StringBuilder();
            if (Data != null)
            {
                if (Data.Count > 0)
                {
                    foreach (var item in Data)
                    {
                        data.AppendFormat("&{0}", item);
                    }
                }
            }
            if (IsChecked)
            {
                switch (CurrentRequestType)
                {
                    case HttpMethod.Get:
                        Navigator.Instance.Get(Url, success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                    case HttpMethod.Head:
                        Navigator.Instance.Head(Url, success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                    case HttpMethod.Post:
                        Navigator.Instance.Post(Url, data.ToString(), success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                    case HttpMethod.Put:
                        Navigator.Instance.Put(Url, data.ToString(), success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                    case HttpMethod.Delete:
                        Navigator.Instance.Delete(Url, data.ToString(), success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                }
            }
            else
            {
                switch (CurrentRequestType)
                {
                    case HttpMethod.Get:
                        Navigator.Instance.GetAsync(Url, success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                    case HttpMethod.Head:
                        Navigator.Instance.HeadAsync(Url, success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                    case HttpMethod.Post:
                        Navigator.Instance.PostAsync(Url, data.ToString(), success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                    case HttpMethod.Put:
                        Navigator.Instance.PutAsync(Url, data.ToString(), success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                    case HttpMethod.Delete:
                        Navigator.Instance.DeleteAsync(Url, data.ToString(), success => Result = success, falied => MessageBox.Show(falied.Message, "Отправка запросов", MessageBoxButton.OK, MessageBoxImage.Error));
                        break;
                }
                
            }
            switch (CurrentRequestType)
            {
                case HttpMethod.Get:
                    break;
            }
        }

        private bool CanSend(object sender)
        {
#if NET_4_5
            return !string.IsNullOrWhiteSpace(Url);
#else
            return !string.IsNullOrEmpty(Url);
#endif
        }

        #endregion

        #region AddParametrCommand

        public DelegateCommand AddParameterCommand { get; private set; }

        private void AddParameter(object sender)
        {
            var parameterWindow = new ParameterControl();
            parameterWindow.ShowDialog();
        }

        private bool CanAddParameter(object sender)
        {
            return Data != null;
        }

        #endregion
    }
}